package com.example.server.controller;

import com.example.server.entity.Address;
import com.example.server.redis.RedisService;
import com.example.server.service.AddressService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("address")
public class AddressController extends BaseController{
    private final AddressService addressService;
    @Autowired
    private RedisService redisService;

    public AddressController(AddressService addressService) {
        this.addressService = addressService;
    }

    @PostMapping("create")
    private int create(@RequestBody Address address){
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        if(address.getSta() == 1){
            addressService.modifyOne("modifyZero",uid);
        }
        address.setUid(uid);
        return addressService.create(address);
    }

    @GetMapping("queryByUid")
    private List<Address> queryByUid(){
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        return addressService.queryList("queryByUid",uid);
    }

    @PutMapping("modifyAdr")
    private int modifyAdr(@RequestBody Address address){

        return addressService.modify(address);
    }

    @PutMapping("modifySta")
    private int modifySta(@RequestBody Address address){
        System.out.println("-------");
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        addressService.modifyOne("modifyZero",uid);
        return addressService.modifyOne("modifyOne",address);
    }
    @DeleteMapping("remove/{id}")
    private int remove(@PathVariable int id){
        return addressService.removeById(id);
    }
    @GetMapping("queryId/{id}")
    private Address queryId(@PathVariable int id){
        Address address = addressService.queryById(id);
        return address;
    }
}
